﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerCollider : MonoBehaviour
{

    
    public int maxHitPoints = 3;
    public float immunityTime = 1.0f;
    public Slider hitPointsSlider;

    private float hitTime;
    private bool isImmune = false;
    private int hitPoints;

    void Start()
    {
        hitPoints = maxHitPoints;
        hitPointsSlider.maxValue = maxHitPoints;
        hitPointsSlider.value = hitPoints;
    }

    void Update()
    {
        if (isImmune)
        {
            if (Time.time - hitTime > immunityTime) //immunity is over
            {
                isImmune = false;
            }
        }

        if (transform.position.y < -10)
        {
            Application.LoadLevel(0);
        }

    }

    public void TakeDamage(int damage)
    {
        Debug.Log("TakeDamage from player: " + hitPoints + " " + isImmune);
        if (!isImmune)
        {

            hitTime = Time.time;
            isImmune = true;

            hitPoints -= damage;
            hitPointsSlider.value = hitPoints;
            if (hitPoints < 1)
            {
                Application.LoadLevel(0);
            }
        }
    }

}


